package com.owbtgad.common.base

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import java.lang.reflect.ParameterizedType

/**
 * 使用 DataBind 并使用ViewModel
 * @param <VDB>
 * @param <VM> 继承BaseViewModel
</VM></VDB> */
abstract class BaseMActivity<VDB : ViewDataBinding, VM : BaseViewModel> :
    BaseDActivity<VDB>() {
    protected lateinit var mViewModel: VM

    override fun onCreate(savedInstanceState: Bundle?) {
        createViewModel()
        super.onCreate(savedInstanceState)
    }

    fun createViewModel() {
        if (mViewModel == null) {
            val type = javaClass.genericSuperclass
            if (type is ParameterizedType){
                type.actualTypeArguments[1]?.let {
                    mViewModel = ViewModelProvider(this).get(it as Class<VM>)
                }
            }
//            mViewModel = ViewModelProvider(this).get(modelClass as Class<VM>)
        }
    }
}